package wef.PW.module;

import wef.PW.module.model.PLCL;
import wef.PW.utils.SendReward;

import java.io.*;
import java.net.Socket;

/**
 * 传入ip地址和端口号创建连接并返回socket对象
 */
public class EspConnect {

    private String IP;
    private int PORT;
    private Socket socket;
    PLCL plcl;

    public EspConnect(String ip, int port, PLCL plcl){

        this.IP = ip;
        this.PORT = port;
        this.plcl = plcl;
        EspPassage(ip, port);
    }

    private void EspPassage(String ip, int port){

        try {
            this.socket = new Socket(ip, port);
            this.plcl.setSocket(socket);
            SendReward.Send(socket, "680407000000");
            RewardReceive rewardReceive = new RewardReceive(socket);
            this.plcl.setRewardReceive(rewardReceive);
        }catch (IOException e){
            e.printStackTrace();
            try {
                socket.close();
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        }
    }

    public String getIP() {
        return IP;
    }

    public int getPORT() {
        return PORT;
    }

    public Socket getSocket() {
        return socket;
    }

    public void close(){
        try {
            this.socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
